package com.neiquan.neiquandemo.utils;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import org.haitao.common.utils.StringUtils;

public class DownloadImgService extends Service {
    private String downloadUrl;
    private String name;
    private String filePath;
    public DownloadImgService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    Object o = msg.obj;
                    Log.e("Service", o.toString());
                    Log.e("Service","下载成功");
//                    filePath = o.toString();
//                    CacheManager.getInstance().saveNewByPageFlag(SplashActivity.splashADVideo, filePath);
//                    tv_load.setText("打开");
//                    List<String> list = PreferenceHelper.getList("sendfilelist" + LoginKeyUtil.getBizUserId());
//                    list.add(name + FILETAG + o.toString());
//                    PreferenceHelper.setList("sendfilelist" + LoginKeyUtil.getBizUserId(), list);
//
//                    FileUtil.openAndroidFile(OnlinePreviewActivity.this, o.toString());
//
//                    Intent intent1 = new Intent();
//                    intent1.putExtra("id", getIntent().getStringExtra("id"));
//                    setResult(GroupFileActivity.REQUEST_CODE_LIST, intent1);
                    break;
                case 0:
                    Log.e("Service","文件路径不对或已被删除");
//                    CacheManager.getInstance().saveNewByPageFlag(SplashActivity.splashADVideo, "");
//                    Toast.makeText(OnlinePreviewActivity.this, "下载失败", Toast.LENGTH_LONG).show();
                    break;
                case 2:
//                    progressBar.setProgress(msg.arg1);
//                    tv_pro.setText(msg.arg1 + "%");
//                    Log.e("Service", msg.arg1 + "%");
                    break;
            }
        }
    };




    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(intent!=null){
            downloadUrl = intent.getStringExtra("downloadUrl");
            name = intent.getStringExtra("name");
            filePath = intent.getStringExtra("filePath");
//            Log.e("Service",downloadUrl+ "   " +name +"  " + filePath);
            DownloadUtil.get().downloadImg(this,downloadUrl,name, filePath,new DownloadUtil.OnDownloadListener() {
                @Override
                public void onDownloadSuccess(String path) {
                    Message message = Message.obtain();
                    message.what = 1;
                    message.obj = path;
                    handler.sendMessage(message);
                    Log.e("Service",filePath);
                }

                @Override
                public void onDownloading(int progress) {
                    Message message = Message.obtain();
                    message.what = 2;
                    message.arg1 = progress;
                    handler.sendMessage(message);
                    Log.e("Service",progress+"..");
                }

                @Override
                public void onDownloadFailed(Exception e) {
                    if (!StringUtils.isEmpty(e.getMessage())){
                        Message message = Message.obtain();
                        message.what = 0;
                        message.obj = e;
                        handler.sendMessage(message);
                        Log.e("Service",e.getMessage());
                    }
                }
            });
        }
        return super.onStartCommand(intent, flags, startId);
    }
}
